package com.junxian.springboot03.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * 回环栅栏：加法计时器
 */
public class CyclicBarrierTest {

    public static void main(String[] args) {

        //集齐七颗灵珠之后召唤神龙
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, new Runnable() {
            @Override
            public void run() {
                System.out.println("你可以召唤神龙");
            }
        });

        for (int i = 0; i <7 ; i++) {
            new Thread(()->{
                System.out.println("收集了"+Thread.currentThread().getName());
                try {
                    int await = cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }, "编号为"+i+"的龙珠").start();
        }

    }

}
